//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// GDI drawing helpers.
//

# include "StdAfx.h"
# include "DrawHelpers.h"
# include "fast_floor.h"
# include "Globals.h"

void DrawLine (HDC _hDC, int _x0, int _y0, int _x1, int _y1) {
	POINT pts [2] = {{_x0, _y0}, {_x1, _y1}};
	::Polyline (_hDC, pts, ARRAY_SIZE (pts));
}

void DrawSquare (HDC _hDC, int _x, int _y, int _nSize) {
	::Rectangle (_hDC, _x - _nSize, _y - _nSize, _x + _nSize + 1, _y + _nSize + 1);
}

void DrawDiamond (HDC _hDC, int _x, int _y, int _nSize) {
	const POINT pts [4] = {
		{_x - _nSize, _y},
		{_x,          _y + _nSize},
		{_x + _nSize, _y},
		{_x,          _y - _nSize},
	};
	::Polygon (_hDC, pts, 4);
}

void DrawCross (HDC _hDC, int _x, int _y) {
	DrawCross (_hDC, _x, _y, c_iCrossSize);
}

void DrawCross (HDC _hDC, int _x, int _y, int _nSize) {
	::MoveToEx (_hDC, _x - _nSize,     _y - _nSize, NULL);
	::LineTo   (_hDC, _x + _nSize + 1, _y + _nSize + 1);

	::MoveToEx (_hDC, _x - _nSize,     _y + _nSize, NULL);
	::LineTo   (_hDC, _x + _nSize + 1, _y - _nSize - 1);
}

void DrawText2 (HDC _hDC, int _x, int _y, const char * _strText, COLORREF _colText) {
	const size_t cLen = ::strlen (_strText);

	::SetBkMode (_hDC, TRANSPARENT);

	if (g_bNT) {
		::SetTextColor (_hDC, c_colWhite);

		POLYTEXT pt [4];
		pt [0].x = _x - 1;
		pt [0].y = _y - 1;
		pt [1].x = _x + 1;
		pt [1].y = _y - 1;
		pt [2].x = _x - 1;
		pt [2].y = _y + 1;
		pt [3].x = _x + 1;
		pt [3].y = _y + 1;
		for (size_t c = 0; c < 4; ++ c) {
			pt [c].n = cLen;
			pt [c].lpstr = _strText;
			pt [c].uiFlags = 0;
			pt [c].pdx = NULL;
		}
		::PolyTextOut (_hDC, pt, 4);
	}

	::SetTextColor (_hDC, _colText);
	::TextOut (_hDC, _x, _y, _strText, cLen);
}

void DrawCompassArrow (HDC _hDC, int _x0, int _y0, float _fAzimuth, int _l) {
	const float fCosA = ::cos (_fAzimuth);
	const float fSinA = ::sin (_fAzimuth);

	//
	// Draw north arrow.
	//

	const int h1 = 5, h2 = _l;

	POINT pts [3];
	pts [0].x = _x0 + fast_floor (h1*fCosA + .5f);
	pts [0].y = _y0 + fast_floor (h1*fSinA + .5f);
	pts [1].x = _x0 - fast_floor (h1*fCosA + .5f);
	pts [1].y = _y0 - fast_floor (h1*fSinA + .5f);
	pts [2].x = _x0 + fast_floor (h2*fSinA + .5f);
	pts [2].y = _y0 - fast_floor (h2*fCosA + .5f);

	::SelectObject (_hDC, g_hbrBlue);
	::Polygon (_hDC, pts, 3);

	//
	// Draw south arrow.
	//

	pts [0].x = _x0 - fast_floor (h1*fCosA + .5f);
	pts [0].y = _y0 - fast_floor (h1*fSinA + .5f);
	pts [1].x = _x0 + fast_floor (h1*fCosA + .5f);
	pts [1].y = _y0 + fast_floor (h1*fSinA + .5f);
	pts [2].x = _x0 - fast_floor (h2*fSinA + .5f);
	pts [2].y = _y0 + fast_floor (h2*fCosA + .5f);

	::SelectObject (_hDC, g_hbrRed);
	::Polygon (_hDC, pts, 3);
}
